package com.ds.seqlist.LeetCode;

public class Offer22_deleteLastKthNode {
    // 快慢指针的衍生问题
    public ListNode getKthFromEnd(ListNode head, int k) {
        // 写边界
        if (head == null || k <= 0) {
            return null;
        }
        ListNode fir = head;
        ListNode sec = head;
        int count = 0;
        while (fir != null) {
            // 先让fir走k步
            fir = fir.next;
            count ++;
            if (count >= k + 1) {
                sec = sec.next;
            }
        }
        // k > count,不存在解
        if (k > count) {
            return null;
        }
        return sec;
    }

}
